package cn.itsource.org.controller;

import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.basic.utils.PageResult;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {


    @Autowired
    private IEmployeeService employeeService;

    @GetMapping
    public List<Employee> findAll(){
        return employeeService.query();
    }

    @GetMapping("/email/{id}")
    public AjaxResult email(@PathVariable("id") Long id ){
        System.out.println(id);
        //根据id 获取对应的员工

        //把员工的状态改为激活


        return AjaxResult.me();
    }

    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Employee employee){
        System.out.println(employee);
        try {
            if (employee.getId()==null){
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
        return new AjaxResult();
    }
    @PostMapping
    public PageResult<Employee> findAll(@RequestBody EmployeeQuery query){
        System.out.println(query);
        return employeeService.queryPage(query);
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
        } catch (Exception e) {
            return new AjaxResult(e.getMessage());
        }
        return new AjaxResult();
    }
   
}
